/**
 * The MIT License (MIT)
 * Copyright (c) 2021 铭软科技(mingsoft.net)
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package net.mingsoft.msend.service;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import net.mingsoft.basic.exception.BusinessException;
import net.mingsoft.msend.entity.TemplateEntity;
import net.mingsoft.msend.util.SendUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

/**
 * 发送基础类
 * @author 铭飞团队
 * @version
 * 版本号：100-000-000<br/>
 * 创建日期：2021-05-15<br/>
 * 历史修订：<br/>
 */
public abstract class BaseSendService {
    protected static String MAIL = "mail", SMS = "sms";
    protected static final Logger LOG = LoggerFactory.getLogger(SendUtil.class);

    /**
     * 发送
     *
     * @param config 配置的类型
     * @param toUser     接收用户
     * @param template   模板内容
     * @param values     根据values.key值替换替换模版里面内容的${key}，
     * @return
     */
    public abstract boolean send(Map<String,String> config, String[] toUser, TemplateEntity template, Map values);

    /**
     * 根据文本内容渲染模板
     *
     * @param root    参数值
     * @param content 模板内容
     * @return 渲染后的内容
     */
    public static String rendering(Map root, String content) {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
        StringTemplateLoader stringLoader = new StringTemplateLoader();
        stringLoader.putTemplate("template", content);
        cfg.setNumberFormat("#");
        cfg.setTemplateLoader(stringLoader);

        Template template = null;
        try {
            template = cfg.getTemplate("template", "utf-8");
            StringWriter writer = new StringWriter();
            template.process(root, writer);
            return writer.toString();
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("消息模板不存在");
        } catch (TemplateException e) {
            e.printStackTrace();
            throw new BusinessException("消息模板解析错误");
        }

    }
}
